<?php
class Offer_help extends CI_Controller{
	public function __construct()
	{
		parent::__construct();
		//登录校验
		$this->common->check_login();
		//通过user_model的get_user_info_id方法获取个人信息
		$this->user_info = $this->user->get_user_info_id($_SESSION['user']['id']);

		$this->load->model('order_model');
	}

	/**
	 * 提供帮助
	 */
	public function index()
	{
		//测试
		//$this->user->user_team_update('得道者',8);
		//var_dump($this->user_info);
		$this->load->helper('form');
		
		

		$data['user'] = $this->user_info;

		$data['score_level'] = $this->common->get_user_level($this->user_info['score']);

		
		$data['left'] = $this->load->view('left', '', TRUE);

		$data['menu'] = $this->load->view('menu', '', TRUE);

		$data['dakuan_type'] = array('未打款','已经打款','拒绝打款');

		$data['order_list'] = $this->order_model->offer_order_log_list_uid($this->user_info['id']);
		//var_dump($data['order_list']);
		$this->load->view('offer_help', $data, FALSE);
	}

	/**
	 * 信息提交
	 */
	public function info_post()
	{
		if(empty($_POST) OR empty($_POST['money']))
		{
			exit($this->common->alert_msg_goback('参数错误'));
		}

		$num = $this->order_model->get_order_dakuan($this->user_info['id']);

		$offer = $this->order_model->offer_order_row_id($this->user_info['id']);

		if(!empty($num) || !empty($offer))
		{
			exit($this->common->alert_msg_goback("还有尚未匹配的公益项目!"));
		}

		$s_money = 2000;

		$b_money = 10000;

		if($_POST['money'] < $s_money)
		{
			exit($this->common->alert_msg_goback("金额不能小于$s_money"));
		}

		if($_POST['money'] > $b_money)
		{
			exit($this->common->alert_msg_goback("金额不能小于$b_money"));
		}


		//要使用的公益币
		$use_public_money = $_POST['money']/1000;

		if(!is_int($use_public_money))
		{
			//exit;
			exit($this->common->alert_msg_goback('金额是1000的整数倍。'));
		}

		if( $use_public_money > $this->user_info['public_money'])
		{
			exit($this->common->alert_msg_goback('公益币余额不足'));
		}

		//新增订单
		$bianhao = $this->common->getnumChar ( 7 );

		$user_lv = $this->common->get_user_level($this->user_info['score']);

		$result = $this->order_model->offer_order_insert($this->user_info['id'],$_POST['money'],$bianhao,$user_lv['id']);

		if($result)
		{
			//更新公益币
			$public_money = $this->user_info['public_money'] - $use_public_money;

			$this->user->update_public_money($this->user_info['id'],$public_money);

			exit($this->common->alert_msg_url('提交成功',site_url('offer_help')));
		}
		else
		{
			exit($this->common->alert_msg_goback('提交失败'));
		}

	}

	/**
	 * ajax更新order_log表的出场日期
	 */
	public function ajax_update_day_num()
	{

		if(empty($_POST))
		{
			echo 0;exit;
		}
		$id = $_POST['id'];

		$day_num = $_POST['day_num'];

		$result = $this->order_model->order_log_update_day_num($day_num,$id);

		if($result)
		{
			echo 1;exit;
		}
		else
		{
			echo 0;exit;
		}
	}

	/**
	 * 更新打款状态
	 */
	public function change_dakuan_type($type,$id,$up_account='')
	{
		if(empty($type) || empty($id))
		{
			exit($this->common->alert_msg_goback('参数错误'));
		}

		$log = $this->order_model->get_order_log_id($id);

		if(empty($log))
		{
			exit($this->common->alert_msg_goback('匹配不存在'));
		}

		$offer = $this->order_model->offer_order_by_id($log['offer_order_id']);

		if(empty($offer) || $offer['user_id'] != $this->user_info['id'])
		{
			exit($this->common->alert_msg_goback('提供帮助信息不存在'));	
		}

		//24小时
		$sy_time = 24 - floor((time() - $log['create_time'])/3600);

		 if($sy_time <= 0)
		 {
		 	exit($this->common->alert_msg_goback('已过期'));
		 }
		//更新打款状态
		$result = $this->order_model->update_order_log_dakuan($type,$id);
		//var_dump($result);exit;

	   	if($result)
	   	{
			if($type == 1)
			{
				
				$accept = $this->order_model->accept_order_by_id($log['accept_order_id']);

				$users = $this->user->get_user_info_id($accept['user_id']);
				

				echo $this->common->alert_msg_url('确认打款',site_url('offer_help'));
				// 短信
				$mobile = $users['mobile'];
				$content = '会员已完成对您的打款操作，请第一时间登陆平台进行核实确认。';
				$this->common->send_sms($mobile,$content);
				exit;
			}
			else
			{

				//匹配订单传给上级
				//上级再拒绝，减匹配金额20%的静态收益，把获得帮助订单重新排列
				if($offer['up_user_id'] > 0){
					//更新旧订单信息
					$this->order_model->offer_order_on_off_userid($offer['id'],-1);
					//拒绝减20%
					$new_money = $offer['money']*0.2;
					//更新
					$this->user->update_minus_user_money(0,$new_money,$offer['user_id']);
					//更新接受帮助订单金额
					$log_list = $this->order_model->order_log_list(0,$offer['id']);

					foreach ($log_list as $k => $v) {
						//$this->order_model->order_log_insert($insert_id,$v['accept_order_id'],$v['money'],$bianhao);
						$this->order_model->update_accept_money($v['accept_order_id'],$v['money']);
					}
					
					//扣款
					exit($this->common->alert_msg_goback('静态金额扣款'.$new_money));

				}else{
					$rr =  $this->user->kick_user($this->user_info['id']);
					
					//封号后退出
					if($rr)
					{
						
						$accept = $this->order_model->accept_order_by_id($log['accept_order_id']);
						
						$users = $this->user->get_user_info_id($accept['user_id']);
						
						$up_users = $this->user->get_uesr_info($users['up_account']);
						
						$this->order_model->offer_order_pay_off($offer['id']);

						echo $this->common->alert_msg_url('已封号',site_url('login/logout'));

						$mobile = $up_users['mobile'];

						$content = '您的直推未按时间完成合作，请于4小时内，登陆平台团队合作记录中，进行相关操作。';

						$this->common->send_sms($mobile,$content);
						
						exit;
					}
				}
				
				//提示
				//exit($this->common->alert_msg_url('拒绝打款',site_url('offer_help')));
			}
	   	}else
	   	{
	   		exit($this->common->alert_msg_url('更新失败',site_url('offer_help')));
	   	}
	}

	/**
	 * 上传图片
	 */
	public function img_upload()
	{
		$config['upload_path']      = './uploads/';           //这个路径很重要

        $config['allowed_types']    = '*';

        $config['max_size']     = 100000;

        $config['overwrite'] = TRUE;

        //$config['encrypt_name'] = TRUE;
        $config['file_name']     = $this->user_info['id'].'_'.time().rand(100,999).".png";

        $this->load->library('upload', $config);

        if($this->upload->do_upload('userfile'))
        {

			$data['upload_data']=$this->upload->data();  //文件的一些信息

             $img=$data['upload_data']['file_name'];           //取得文件名

             echo $img;

             exit;
        }
        else
        {
        	echo 0;
            //echo $this->upload->display_errors();
            exit;
        }
	}

	/**
	 * 图片更新
	 */
	public function update_img()
	{
		if(empty($_POST) || empty($_POST['img_url']) || empty($_POST['order_id']))
		{
			exit($this->common->alert_msg_goback('参数错误'));
		}

		$result = $this->order_model->order_log_update_img($_POST['img_url'],$_POST['order_id']);

		if($result)
		{
			exit($this->common->alert_msg_url('提交成功',site_url('offer_help')));
		}
		else
		{
			exit($this->common->alert_msg_url('提交失败',site_url('offer_help')));
		}

	}

	/**
	 * 每次确认向上追溯5代按不同等级分红，每天有上限
	 *订单日志ID
	 *$dai 第几代 one、two、three、four、five
	 *$up_account收款方的上级
	 */
	private function five_generation($offer_order_id,$dai,$up_account,$money)
	{
		$info = $this->user->get_uesr_info($up_account);
		//存在上代
		if(!empty($info))
		{
			$team_arr = $this->common->team_lv_rate();

			$team_lv = $team_arr[$info['team_lv']];
			//var_dump($team_lv);
			$rate = $team_lv[$dai];
			//var_dump($rate);
			//此代分红利率不为0
			if(!empty($rate))
			{
				//此笔交易未分红
				$log = $this->order_model->get_team_log($info['id'],$offer_order_id);

				if(empty($log))
				{
					$dk_money = $this->order_model->get_offer_order_dakuan_money($offer_order_id);

					if(empty($dk_money) || $dk_money['sum_money'] != $money )
					{
						return TRUE;
					}


					//echo 111;
					$max = $team_lv['max'];
					//获取当日金额
					$sum_money = $this->order_model->get_team_log_day_money($info['id']);
					//烧穿判断 获取上代的提供帮助订单金额
					$up_info = $this->order_model->get_new_offer_order($info['id']);
					//不存在帮助订单
					if(empty($up_info))
					{
						return TRUE;
					}
					elseif($up_info['money'] < $money)
					{
						$money = $up_info['money'];
					}
					//分红money
					$fh_money = $money * ($rate/100) ;
					//先加再判断是否超上限
					$sum_money += $fh_money;
					//echo $sum_money;
					//未超过当日分红上限
					if($max >= $sum_money)
					{
						//$data['ol_id']       = $ol_id;
						$data['user_id']     = $info['id'];
						$data['dai']         = $dai;
						$data['money']       = $fh_money;
						$data['create_time'] = time();
						//参与分红用户的上代账号
						$data['up_account']  = $info['up_account'];

						$data['offer_order_id'] = $offer_order_id;
						//r_dump($data);
					    $this->db->insert('pc_ddth_team_log', $data);

					    $this->user->update_user_tgmoney($fh_money,$info['id']);
					}

					//全部匹配都打款
					$this->order_model->offer_order_pay_on($offer_order_id);

				}
			}

			if($dai == "one")
			{
				$this->five_generation($offer_order_id,  'two',$info['up_account'],$money);
			}
			elseif($dai == "two")
			{
				$this->five_generation($offer_order_id,'three',$info['up_account'],$money);
			}
			elseif($dai == 'three')
			{
				$this->five_generation($offer_order_id,'four',$info['up_account'],$money);
			}
			elseif($dai == 'four')
			{
				$this->five_generation($offer_order_id,'five',$info['up_account'],$money);
			}
			else{
				return TRUE;
			}

		}else
		{
			return TRUE;
		}


	}
}